//
//  PhonePickView.m
//  dongfengTest
//
//  Created by wxt on 2021/3/24.
//

#import "PhonePickView.h"

@implementation PhonePickView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame arr:(NSArray *)arrData
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setBackgroundColor:[UIColor whiteColor]];
        self.arr = [[NSMutableArray alloc ] initWithArray:arrData];
        [self initCtrl:frame];
    }
    return self;
}

-(void)initCtrl:(CGRect)frame
{
    
    
    
    UIWindow * window = [[UIApplication sharedApplication]keyWindow];
    //2. 创建背景视图
    self.bgView = [[UIView alloc]init];
    
    self.bgView.frame = window.bounds;
    self.bgView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    self.bgView.userInteractionEnabled = YES;
    [window addSubview:  self.bgView];
    
    UIButton *btnCancel = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, 80, 40)];
    [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [btnCancel setTitleColor:Bcolor forState:UIControlStateNormal];
    [btnCancel addTarget:self action:@selector(onButtonCancel) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btnCancel];
    
    UIButton *btnFinish = [[UIButton alloc ] initWithFrame:CGRectMake(kWidth-80, 0, 80, 40)];
    [btnFinish setTitle:@"完成" forState:UIControlStateNormal];
    [btnFinish setTitleColor:JhColor(75, 156, 252) forState:UIControlStateNormal];
    [btnFinish addTarget:self action:@selector(onButtonFinish) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btnFinish];
    
    UIPickerView *pickerView = [[UIPickerView alloc ] initWithFrame:CGRectMake(0, 40, frame.size.width, frame.size.height-40)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [pickerView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:pickerView];
    
    [self.bgView addSubview:self];
    
    
}

-(void) onButtonCancel
{
    if (self)
    {
        [self.bgView removeFromSuperview];
    }
}

-(void) onButtonFinish
{
    if ([self.delegate respondsToSelector:@selector(selectIndex:)]) {
        [self.delegate selectIndex:_indexSelect];
    }
    //点击完成后，关闭当前view
    [self onButtonCancel];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.arr.count;
}








- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    return [self.arr objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    _indexSelect = row;
    
}
@end
